package com.contract.commonlib.view

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.os.CountDownTimer
import android.text.TextUtils
import android.util.AttributeSet
import androidx.activity.ComponentActivity
import androidx.appcompat.widget.AppCompatTextView
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import com.contract.commonlib.R

/**
 *倒计时
 *@author zhengjun
 *@time 2019/4/10 13:49
 */
class CountDownTextView : AppCompatTextView, LifecycleObserver {
    private var mTotalTime = 60 * 1000L
    private var mIntervalTime = 1 * 1000L
    private var mCountTag: String? = "s"
    private var mEndTip: String? = "s"
    private  var mTimer: CountDownTimer? = null
    private var mCountColor: Int
    private var mEndColor: Int
    private var mCountBg : Drawable?
    private var mEndBg : Drawable?

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.CountDownTextView)
        mTotalTime = typedArray.getInt(R.styleable.CountDownTextView_total_second, 60) * 1000L
        mIntervalTime = typedArray.getInt(R.styleable.CountDownTextView_interval_second, 1) * 1000L
        val countTag = typedArray.getString(R.styleable.CountDownTextView_count_tag)
        mCountTag = if (TextUtils.isEmpty(countTag)) mCountTag else countTag
        val endTip = typedArray.getString(R.styleable.CountDownTextView_end_tip)
        mEndTip = if (TextUtils.isEmpty(endTip)) mEndTip else endTip
        mCountColor = typedArray.getColor(R.styleable.CountDownTextView_count_color, Color.BLACK)
        mEndColor = typedArray.getColor(R.styleable.CountDownTextView_end_color, Color.BLACK)
        mCountBg = typedArray.getDrawable(R.styleable.CountDownTextView_count_bg)
        mEndBg = typedArray.getDrawable(R.styleable.CountDownTextView_end_bg)
        typedArray.recycle()
        init(context)
    }


    /**
     * 开启服务
     */
    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    private fun pageDestroy() {
        mTimer?.cancel()
        mTimer = null
    }


    private fun init(context: Context?) {
        context?.let {
            if (it is ComponentActivity) {
                it.lifecycle.addObserver(this)
            }
        }
        mTimer = object : CountDownTimer(mTotalTime, mIntervalTime) {
            override fun onFinish() {
                isEnabled = true
                if (mEndColor != Color.BLACK) setTextColor(mEndColor)
                text = mEndTip
                if (mEndBg!=null){
                    background = mEndBg
                }
            }

            @SuppressLint("SetTextI18n")
            override fun onTick(millisUntilFinished: Long) {
                if (isEnabled) {
                    isEnabled = false
                    setTextColor(mCountColor)
                }
                if (mCountBg!=null){
                    background = mCountBg
                }
                text = "${millisUntilFinished / 1000L}$mCountTag"
            }
        }
    }

    fun start() {
        mTimer?.start()
    }
}